package com.app.service.impl;

import com.app.mapper.*;
import com.app.model.*;
import com.app.service.DepartmentService;
import com.app.service.ProjectProgressService;
import com.app.util.StringTool;
import com.app.vo.DepartmentVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ProjectProgressService projectProgressService;
    @Autowired
    private PlanMapper planMapper;
    @Autowired
    private StriveForFundsMapper striveForFundsMapper;
    @Autowired
    private PlanBusinessMapper planBusinessMapper;
    @Autowired
    private AttractInvestmentMapper attractInvestmentMapper;
    @Autowired
    private SystemCategoryMapper systemCategoryMapper;

    @Override
    public DepartmentModel add(Object name, Integer sort,Integer type) {
        DepartmentModel model = new DepartmentModel();
        model.setUnitName((String) name);
        model.setSort(sort);
        model.setType(type);
        model.setCreate_time(LocalDateTime.now());
        departmentMapper.insert(model);
        return model;
    }

    @Override
    public DepartmentModel update(String name, int sort, int id,int type) {
        DepartmentModel model = departmentMapper.selectById(id);
        model.setUnitName(name);
        model.setSort(sort);
        model.setType(type);
        model.setUpdate_time(LocalDateTime.now());
        departmentMapper.updateById(model);
        return model;
    }

    @Override
    public void deleteDepartmentById(int id) {
        departmentMapper.deleteById(id);
    }

    @Override
    public DepartmentVo departmentList(DepartmentModel model) {
        List<DepartmentModel> listByModel = getListByModel(model);

        List<SystemModel> departmentClass = getDepartmentClass();
        for (int i = 0; i < departmentClass.size(); i++) {
            for (int i1 = 0; i1 < listByModel.size(); i1++) {
                if (listByModel.get(i1).getType()==departmentClass.get(i).getId()){
                    listByModel.get(i1).setTypeText(departmentClass.get(i).getName());
                }
            }
        }
        int countByModel = getCountByModel(model);
        DepartmentVo vo = new DepartmentVo();
        vo.setList(listByModel);
        vo.setCount(countByModel);
        return vo;
    }

    @Override
    public List<DepartmentModel> getListByModel(DepartmentModel model) {
        return departmentMapper.getListByModel(model);
    }

    @Override
    public int getCountByModel(DepartmentModel model) {
        return departmentMapper.getCountByModel(model);
    }


    @Override
    public DepartmentVo List(DepartmentModel model) {
        List<DepartmentModel> list = getList(model);
        List<SystemModel> departmentClass = getDepartmentClass();
        for (int i = 0; i < departmentClass.size(); i++) {
            for (int i1 = 0; i1 < list.size(); i1++) {
                if (list.get(i1).getType()==departmentClass.get(i).getId()){
                    list.get(i1).setTypeText(departmentClass.get(i).getName());
                }
            }
        }
        int count = getCount(model);
        DepartmentVo vo = new DepartmentVo();
        vo.setList(list);
        vo.setCount(count);
        return vo;
    }

    @Override
    public List<DepartmentModel> getList(DepartmentModel model) {
        return departmentMapper.getList(model);
    }

    @Override
    public int getCount(DepartmentModel model) {
        return departmentMapper.getCount(model);
    }

    @Override
    public List<SystemModel> getDepartmentClass() {

        return departmentMapper.getDepartmentClass();
    }

    @Override
    public List<DepartmentModel> getDepartmentModelAll() {
        return departmentMapper.selectList(null);
    }

    @Override
    public List<PlanFundsModel> getModelByStatus(Integer status) {
        if(status==1)
        {
            status = 71;
        }else if(status==2)
        {
            status = 72;
        } else if(status==3)
        {
            status = 73;
        }
        List<DepartmentModel> departmentModelList = getDepartmentModelAll();
        List<SystemModel> systemModelList = projectProgressService.getSystemAll();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        List<PlanFundsModel> planFundsModelList = getPlanFundsStatus(status,String.valueOf(year));
        List<StriveFundsModel> striveFundsModelList = getStriveFundsStatus();
        Double fundNum = 0.0;

        for (int j = 0; j < planFundsModelList.size(); j++) {
            for (int i = 0; i < departmentModelList.size(); i++) {
                if(planFundsModelList.get(j).getDepartmentId() == departmentModelList.get(i).getId())
                {
                    planFundsModelList.get(j).setDepartmentText(departmentModelList.get(i).getUnitName());
                    planFundsModelList.get(j).setType(departmentModelList.get(i).getType());
                }
            }
            for (int k = 0; k < systemModelList.size(); k++) {
                if(planFundsModelList.get(j).getType() == systemModelList.get(k).getId())
                {
                    planFundsModelList.get(j).setTypeText(systemModelList.get(k).getName());
                }
            }

            fundNum = planFundsModelList.get(j).getTaskPlan();
            Double striveNum = 0.0;
            for (int k = 0; k < striveFundsModelList.size(); k++) {
                if(planFundsModelList.get(j).getDepartmentId()==striveFundsModelList.get(k).getDepartmentId() && Objects.equals(planFundsModelList.get(j).getYear(), striveFundsModelList.get(k).getYear()))
                {
                    striveNum+=striveFundsModelList.get(k).getAmount();
                }
            }
            if(fundNum>0)
            {
                planFundsModelList.get(j).setAmount(striveNum);
                BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(100));
                double s =  bigDecimal1.multiply(divide(striveNum,fundNum)).doubleValue();
                planFundsModelList.get(j).setTaskCompletion(s);
            }

        }
        return search(planFundsModelList);
    }

    // 方式1: 降序排列
    public List<PlanFundsModel> search(List<PlanFundsModel> list){
        Collections.sort(list, new Comparator<PlanFundsModel>() {
            @Override
            public int compare(PlanFundsModel o1, PlanFundsModel o2) {
                if ((o1.getTaskCompletion() > o2.getTaskCompletion())){
                    return -1;
                }
                if (o1.getTaskCompletion() == o2.getTaskCompletion()){
                    return 0;
                }
                return 1;
            }
        });
        return list;
    }

    @Override
    public List<DepartmentModel> getBusinessByStatus(Integer status) {
        List<DepartmentModel> departmentModelList = getDepartmentModelAll();
        List<SystemModel> systemModelList = projectProgressService.getSystemAll();
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        QueryWrapper<PlanBusinessModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("year", String.valueOf(year));
        List<PlanBusinessModel> planBusinessModelList = planBusinessMapper.selectList(queryWrapper);
        QueryWrapper<BusinessModel> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.select().eq("year", String.valueOf(year));
        List<BusinessModel> businessModelList = attractInvestmentMapper.selectList(queryWrapper2);
        List<DepartmentModel> newDepartmentModelList = new ArrayList<>();
        for (int i = 0; i < departmentModelList.size(); i++) {
            for (int j = 0; j < systemModelList.size(); j++) {
                if(departmentModelList.get(i).getType()==systemModelList.get(j).getId())
                {
                    departmentModelList.get(i).setTypeText(systemModelList.get(j).getName());
                }
            }
            Double fundNum = 0.0;

            for (int j = 0; j < planBusinessModelList.size(); j++) {
                if(planBusinessModelList.get(j).getDepartmentId() == departmentModelList.get(i).getId())
                {
                    fundNum = planBusinessModelList.get(j).getTaskPlan();
                    departmentModelList.get(i).setTaskPlan(fundNum);
                    Double striveNum = 0.0;
                    for (int k = 0; k < businessModelList.size(); k++) {
                        if(planBusinessModelList.get(j).getDepartmentId()==businessModelList.get(k).getDepartmentId() && Objects.equals(planBusinessModelList.get(j).getYear(), businessModelList.get(k).getYear()))
                        {
                            striveNum+=businessModelList.get(k).getAmount();
                        }
                    }
                    if(fundNum>0)
                    {
                        departmentModelList.get(i).setAmount(striveNum);
                        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(100));
                        BigDecimal bigDecimal2 = divide(striveNum,fundNum);
                        double s =  bigDecimal1.multiply(bigDecimal2).doubleValue();
                        departmentModelList.get(i).setTaskCompletion(s);
                    }
                }

            }
            if(departmentModelList.get(i).getTaskCompletion()==null)
            {
                departmentModelList.get(i).setTaskCompletion(0.0);
            }
            if(departmentModelList.get(i).getTaskPlan()==null)
            {
                departmentModelList.get(i).setTaskPlan(0.0);
            }
            if(status==3&&departmentModelList.get(i).getTaskCompletion()<=0&&departmentModelList.get(i).getTaskPlan()>0)
            {
                newDepartmentModelList.add(departmentModelList.get(i));
            }
            if(status==2&&departmentModelList.get(i).getTaskCompletion()<=20&&departmentModelList.get(i).getTaskCompletion()>0&&departmentModelList.get(i).getTaskPlan()>0)
            {
                newDepartmentModelList.add(departmentModelList.get(i));
            }else if(status==1&&departmentModelList.get(i).getTaskCompletion()>20&&departmentModelList.get(i).getTaskPlan()>0){
                newDepartmentModelList.add(departmentModelList.get(i));
            }
        }
        return searchDepartment(newDepartmentModelList);

    }
    // 方式1: 降序排列
    public List<DepartmentModel> searchDepartment(List<DepartmentModel> list){
        Collections.sort(list, new Comparator<DepartmentModel>() {
            @Override
            public int compare(DepartmentModel o1, DepartmentModel o2) {
                if ((o1.getTaskCompletion() > o2.getTaskCompletion())){
                    return -1;
                }
                if (o1.getTaskCompletion() == o2.getTaskCompletion()){
                    return 0;
                }
                return 1;
            }
        });
        return list;
    }
    @Override
    public List<PlanFundsModel> getPlanFundsStatus(Integer status,String year) {
        QueryWrapper<PlanFundsModel> queryWrapper = new QueryWrapper<>();
        if (status > 0) {
            queryWrapper.select().eq("status", status);
        }
        queryWrapper.select().eq("year", year);
        return planMapper.selectList(queryWrapper);
    }

    @Override
    public List<StriveFundsModel> getStriveFundsStatus() {

        return striveForFundsMapper.selectList(null);
    }

    @Override
    public Integer getFundCount(Integer status) {
        QueryWrapper<PlanFundsModel> queryWrapper = new QueryWrapper<>();
        if (status > 0) {
            queryWrapper.select().ne("status", status);
        }
        return planMapper.selectCount(queryWrapper);
    }

    @Override
    public List<SystemModel> getSystemByPid(Integer pid) {
        QueryWrapper<SystemModel> queryWrapper = new QueryWrapper<>();
        if (pid > 0) {
            queryWrapper.select().eq("pid", pid);
        }
        return systemCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<DepartmentModel> getDepartmentList(Integer type) {
        QueryWrapper<DepartmentModel> queryWrapper = new QueryWrapper<>();
        if (type > 0) {
            queryWrapper.select().eq("type", type);
        }
        return departmentMapper.selectList(queryWrapper);
    }

    @Override
    public int getDepartmentCount() {
        return departmentMapper.count();
    }


    /**
     * 除法运算
     * @param num
     * @param total
     * @return
     */
    public static BigDecimal divide(double num, double total) {
        BigDecimal bigDecimal1 = new BigDecimal(String.valueOf(num));
        BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(total));
        return bigDecimal1.divide(bigDecimal2, 2, BigDecimal.ROUND_HALF_UP);
    }


}
